// SmartDial.cpp : Defines the entry point for the application.
//

#include "stdafx.h"
#include "SmartDial.h"
#include <tapi.h>
#include <phone.h>

INT_PTR CALLBACK SmartDialDialog(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
    switch (message)
    {
		case WM_INITDIALOG:
		{
			SHINITDLGINFO shidi;
			SHMENUBARINFO mbi;

			// Make the window full screen
            shidi.dwMask = SHIDIM_FLAGS;
            shidi.dwFlags = SHIDIF_DONEBUTTON | SHIDIF_SIPDOWN | SHIDIF_SIZEDLGFULLSCREEN;
            shidi.hDlg = hDlg;
            SHInitDialog(&shidi);

			// Add a menu bar
			memset(&mbi, 0, sizeof(SHMENUBARINFO));
			mbi.cbSize     = sizeof(SHMENUBARINFO);
			mbi.hwndParent = hDlg;
			mbi.nToolBarId = IDR_MENU;
			mbi.dwFlags = SHCMBF_HMENU;
			mbi.hInstRes   = GetModuleHandle(NULL);
			SHCreateMenuBar(&mbi);

			// Change the Phone Number static control to
			// use a bold font with a much larger font size
			LOGFONT lf;
			memset(&lf, 0, sizeof(LOGFONT));
			HDC hdc = GetDC(NULL);
			lf.lfHeight = -22 * GetDeviceCaps(hdc, LOGPIXELSY) / 72;
			ReleaseDC(NULL, hdc);
			lf.lfWeight = FW_BOLD;
			lf.lfQuality = CLEARTYPE_QUALITY;
			HFONT hFont = CreateFontIndirect(&lf);

			SendDlgItemMessage(hDlg, IDC_NUMBER, WM_SETFONT, (WPARAM)hFont, 0);

            return (INT_PTR)TRUE;
		}

		case WM_CTLCOLORSTATIC:
		{
			// Change the background colour of the phone number static control
			int id = GetWindowLong((HWND)lParam, GWL_ID);
			if (id == IDC_NUMBER)
			{
				// Use GetSysColor to get standard colours which
				// fit with the current color scheme of the device
				COLORREF clrInfoBk = GetSysColor(COLOR_INFOBK);
				COLORREF clrMenuText = GetSysColor(COLOR_MENUTEXT);

				HBRUSH brush = CreateSolidBrush(clrInfoBk);
				SetBkMode((HDC)wParam, TRANSPARENT);
				SetTextColor((HDC)wParam, clrMenuText);
				return (INT_PTR)brush;
			}
		}
		break;

        case WM_COMMAND:
			switch (LOWORD(wParam))
			{
				case IDC_0:
				case IDC_1:
				case IDC_2:
				case IDC_3:
				case IDC_4:
				case IDC_5:
				case IDC_6:
				case IDC_7:
				case IDC_8:
				case IDC_9:
				case IDC_STAR:
				case IDC_POUND:
					if (HIWORD(wParam) == STN_CLICKED)
					{
						TCHAR szBuffer[TAPIMAXDESTADDRESSSIZE];
						TCHAR szChar[] = L"0123456789*#";
						int pos;
	
						// Get the current phone number
						// displayed in the static control
						GetDlgItemText(hDlg, IDC_NUMBER, szBuffer, sizeof(szBuffer));
	
						// Append the next digit
						pos = _tcslen(szBuffer);
						szBuffer[pos++] = szChar[LOWORD(wParam) - IDC_0];
						szBuffer[pos++] = '\0';
					
						// Store the updated phone number
						// in the phone number static control
						SetDlgItemText(hDlg, IDC_NUMBER, szBuffer);
					}
					break;

				case IDC_CLEAR:
					SetDlgItemText(hDlg, IDC_NUMBER, L"");
					break;
				
				case IDC_DIAL:
					{
					PHONEMAKECALLINFO pmci;
					TCHAR szPhoneNumber[TAPIMAXDESTADDRESSSIZE];
	
					// Obtain the current phone number
					GetDlgItemText(hDlg, IDC_NUMBER, szPhoneNumber, sizeof(szPhoneNumber));
	
					// And pass it to PhoneMakeCall in order
					// to make the phone call.
					memset(&pmci, 0, sizeof(pmci));
					pmci.cbSize = sizeof(pmci);
					pmci.dwFlags = PMCF_DEFAULT;
					pmci.pszDestAddress = szPhoneNumber;
					PhoneMakeCall(&pmci);
					}
					break;

				case IDOK:
					EndDialog(hDlg, LOWORD(wParam));
					return (INT_PTR)TRUE;
			}
			break;

        case WM_CLOSE:
            EndDialog(hDlg, message);
            return (INT_PTR)TRUE;

    }

    return (INT_PTR)FALSE;
}

int WINAPI WinMain(HINSTANCE hInstance,
                   HINSTANCE hPrevInstance,
                   LPTSTR    lpCmdLine,
                   int       nCmdShow)
{
	return DialogBox(hInstance, (LPCTSTR)IDD_SMARTDIAL, NULL, SmartDialDialog);
}